var fetch = require('@system.fetch');

var API_ROOT = 'http://apicloud.mob.com';
var headers = {
	'Content-Type': 'application/x-www-form-urlencoded'
};
var key = '27f58a55db282';

function getTimestamp() {
	let nowDate = new Date();
	let hours = nowDate.getHours();
	let minutes = nowDate.getMinutes();
	let seconds = nowDate.getSeconds();
	if (hours < 10) {
		hours = '0' + String(hours);
	}
	if (minutes < 10) {
		minutes = '0' + String(minutes);
	}
	if (seconds < 10) {
		seconds = '0' + String(seconds);
	}
	return String(hours) + String(minutes) + String(seconds);
}

function realFetch(url, data = null, method = 'get') {
	if (data == null) {
		data = {};
	}
	return new Promise((resolve, reject) => {
		fetch.fetch({
			url: API_ROOT + url,
			data: data,
			header: headers,
			method: method,
			success: function(data) {
				resolve(data);
			},
			fail: function(data, code) {
				reject(data, code);
			}
		});
	});
}

function post(url, data = null) {
	return realFetch(url, data, 'post');
}

function get(url, data = null) {
	return realFetch(url, data, 'get');
}

export  function getHoroscope(date,hour) {
		return get('/appstore/horoscope/day',{date:date,hour:hour,key:key}).then((response) => {
			console.log(JSON.parse(response.data));
            return Promise.resolve(JSON.parse(response.data));
        }).catch((err) => {
            return Promise.reject(err);
	})
}
export  function getMarriage(menDate,menHour,womanDate,womanHour) {
    return get('/appstore/marriage/day',{menDate:menDate,menHour:menHour,womanDate:womanDate,womanHour:womanHour,key:key}).then((response) => {
        console.log(JSON.parse(response.data));
        return Promise.resolve(JSON.parse(response.data));
    }).catch((err) => {
        return Promise.reject(err);
    })
}
